package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.enums.ReconciliationType;
import com.xyy.saas.payment.job.reconciliation.ReconciliationJob;
import com.xyy.saas.payment.pojo.dto.ReconcileContext;
import com.xyy.saas.payment.service.PinganLoanService;
import com.xyy.saas.payment.service.reconciliation.ReconciliationExecutor;
import com.xyy.saas.payment.util.DateUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {
    @Autowired
    private PinganLoanService pinganLoanService;

    @Value("${apollo.test.config:success}")
    private String apolloTestConfig;

    @Resource
    private ReconciliationJob reconciliationJob;
    @Resource
    private ReconciliationExecutor reconciliationExecutor;

    @PostMapping(value = "/reconciliationJob")
    public String ReconciliationJob() {
        reconciliationJob.execute(null);
        return "success";
    }

    @PostMapping("/reconciliationExecutor/execute")
    public String reconciliationExecutor(@RequestBody ReconciliationExecutorParam param) {
        ReconcileContext context = new ReconcileContext()
                .setBeginTime(DateUtil.toLocalDateTime(param.getBeginTime()))
                .setEndTime(DateUtil.toLocalDateTime(param.getEndTime()))
                .setBusinessUserIds(param.getBusinessUserIds());
        new Thread(() -> reconciliationExecutor.execute(param.getTypes(), context),
                String.join("-", "reconciliationExecutor", LocalDateTime.now().format(DateUtil.PATTERN_DATE)))
                .start();
        return "done";
    }

    @Data
    public static class ReconciliationExecutorParam {
        private Integer flag;
        private List<ReconciliationType> types;
        private String beginTime;
        private String endTime;
        private List<String> businessUserIds;
    }

    @GetMapping(value = {"/apollo/config"})
    public String getApolloTestConfig() {
        log.info("TestController apolloTestConfig:{}", apolloTestConfig);
        return apolloTestConfig;
    }

    @GetMapping(value = {"/downloadLoanHuanKuanFile"})
    public String downloadLoanHuanKuanFile(@RequestParam(value = "fileDate") String fileDate) {
        pinganLoanService.onlyDownloadLoanHuanKuanFile(fileDate);
        return "success";
    }

    /**
     * 重新下载历史还款计划
     */
    @GetMapping(value = {"/downloadLoanHuanKuanFileHistory"})
    public String downloadLoanHuanKuanFileHistory(@RequestParam(value = "startFileDate") String startFileDate, @RequestParam(value = "analy", defaultValue = "false") boolean analy) throws ParseException, InterruptedException {
        return pinganLoanService.downloadLoanHuanKuanFileHistory(startFileDate, analy);
    }

    /**
     * 重新下载历史还款计划
     */
    @GetMapping(value = {"/history"})
    public String history(@RequestParam(value = "db", defaultValue = "false") boolean db, @RequestParam(value = "clearDb", defaultValue = "false") boolean clearDb) {
        pinganLoanService.dealHistory(db, clearDb);
        return "success";
    }

}
